<template>
  <div class="content">
    <template>
      <el-button type="text" size="small" @click.stop="edit">修改
      </el-button>
      <el-button type="text" size="small" @click.stop="del">删除
      </el-button>
    </template>
  </div>
</template>

<script>

  import { commodityDel } from "../../http/apis/goodsApi/commodity.js";
  import { purchaseDel } from "../../http/apis/goodsApi/purchase.js";
  import { supplierDel } from "../../http/apis/goodsApi/supplier.js";
  import { warehouseDel } from "../../http/apis/goodsApi/warehouse.js";
  
  import { mapState, mapMutations } from 'vuex'
  export default {
    name: 'GoodsToolBtn',
    data() {
      return {};
    },
    props: {
      url: String,
      rowObj: Object,
    },
    created() { },
    computed: {
      ...mapState('goodsStore', ['type']),
    },
    methods: {
      ...mapMutations('goodsStore', ['changeAD']),
      // 跳转修改页面
      edit() {
        this.changeAD()
        this.$router.push({
          path: this.url,
          query: { editmsg: this.rowObj },
        })
      },
      del() {
        this.$confirm('此操作将删除本条信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            console.log(typeof this.rowObj.sh_id);
            if (this.type == 'goods') {
              commodityDel({ goods_id: this.rowObj.goods_id })
                .then((res) => {
                  console.log(res.data);
                  this.$emit('reshow');
                  this.$message({
                    type: "success",
                    message: "删除成功",
                    showClose: true,
                  });
                })
                .catch((err) => {
                  console.log("请求失败:", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                    showClose: true,
                  });
                });
            } else if (this.type == 'purchase') {
              console.log('purchase')
              purchaseDel({ sh_id: this.rowObj.sh_id })
                .then((res) => {
                  console.log(res.data);
                  this.$emit('reshow');
                  this.$message({
                    type: "success",
                    message: "删除成功",
                    showClose: true,
                  });
                })
                .catch((err) => {
                  console.log("请求失败:", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                    showClose: true,
                  });
                });
            } else if (this.type == 'supply') {
              console.log('supply')
              supplierDel({ sh_id: this.rowObj.sh_id })
                .then((res) => {
                  console.log(res.data);
                  this.$emit('reshow');
                  this.$message({
                    type: "success",
                    message: "删除成功",
                    showClose: true,
                  });
                })
                .catch((err) => {
                  console.log("请求失败:", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                    showClose: true,
                  });
                });
            } else if (this.type == 'store_house') {
              console.log('cangku')
              warehouseDel({ sh_id: this.rowObj.sh_id })
                .then((res) => {
                  console.log(res.data);
                  this.$emit('reshow');
                  this.$message({
                    type: "success",
                    message: "删除成功",
                    showClose: true,
                  });
                })
                .catch((err) => {
                  console.log("请求失败:", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                    showClose: true,
                  });
                });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
              showClose: true,
            });
          });
      }
    }
  };
</script>

<style>
</style>